# -*- coding: utf-8 -*-
"""
Matlab .mat data file manager.

Created on Wed Apr 29 12:26:29 2020

@author: Farman
"""


import scipy.io as sio
import numpy as np



def mat2array(mat_file):
    '''
    Read Matlab .mat data file as a numpy array.

    Parameters
    ----------
    mat_file : string.
               Name of Matlab .mat data file to read.

    Returns
    -------
    data in form of numpy array.

    '''
    dict_data = sio.loadmat(mat_file)
    
    keys = dict_data.keys()
    data = {}
    
    for key in keys:
        if key.find('__') > -1:
            continue
        
        data.update({key:dict_data.get(key)})

    return data


#a = mat2array(r'D:\Pavia\Pavia\PaviaCenter\Pavia_gt.mat')


def array2mat(array_name, numpy_array, mat_file):
    '''
    Save numpy array to Matlab .mat file.

    Parameters
    ----------
    array_name: string.
                Numpy array name.
                
    numpy_array : numpy array.
        Numpy array to save.
        
    mat_file : string.
        Matlab .mat file name to save numpy array data.

    Returns
    -------
    None.
    '''
    data = {array_name : numpy_array}
    sio.savemat(mat_file, data)
    return

'''
name = 'test_name'
a = np.array([1,2,3,4,5])
array2mat(name, a, '1.mat')
'''  